package BehavioralPattern.Interpreter;

import java.util.Map;

/**
 * @Classname : SymbolExpression
 * @Description : 运算符号（非终结符号）解析器
 * @Author : chentianyu
 * @Date 2023/3/10 06:40
 */

// 抽象运算符号解析器
public abstract class SymbolExpression extends Expression {
    protected Expression left;
    protected Expression right;

    public SymbolExpression(Expression left, Expression right) {
        this.left = left;
        this.right = right;
    }
}


// 加法解析器
class AddExpression extends SymbolExpression {

    public AddExpression(Expression left, Expression right) {
        super(left, right);
    }

    @Override
    public int interpret(Map<String, Integer> var) {
        return super.left.interpret(var) + super.right.interpret(var);
    }
}


// 减法解析器
class SubExpression extends SymbolExpression {

    public SubExpression(Expression left, Expression right) {
        super(left, right);
    }

    @Override
    public int interpret(Map<String, Integer> var) {
        return super.left.interpret(var) - super.right.interpret(var);
    }
}

